package com.wei.czz.framework.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.framework.common.dao.FileImportFlowDao;
import com.wei.czz.framework.common.entity.FileImportFlowEntity;
import com.wei.czz.framework.common.service.FileImportFlowService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-05-05 10:46:58
 * className: FileImportFlowServiceImpl
 * version: 1.0
 * description:
 */
@Service("fileImportFlowService")
@AllArgsConstructor
public class FileImportFlowServiceImpl extends ServiceImpl<FileImportFlowDao, FileImportFlowEntity>
        implements FileImportFlowService {

    private static final Logger log = LoggerFactory.getLogger(FileImportFlowServiceImpl.class);

    @Override
    public Long saveOrEdit(FileImportFlowEntity fileImportFlow) {
        Long id = fileImportFlow.getId();
        if (Objects.isNull(id)) {

            int count = baseMapper.insert(fileImportFlow);
            log.info("保存文件上传流水完成。count={}", count);
        } else {
            int count = baseMapper.updateById(fileImportFlow);
            log.info("修改文件上传流水完成。count={}", count);
        }
        return id;
    }

    @Transactional
    @Override
    public void batchSave(List<FileImportFlowEntity> fileImportFlowList) {
        boolean bool = this.saveBatch(fileImportFlowList);
        log.info("批量保存文件上传流水完成。bool={}", bool);
    }

    @Override
    public List<FileImportFlowEntity> findList(Long fileImportId) {
        LambdaQueryWrapper<FileImportFlowEntity> fileImportFlowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fileImportFlowLambdaQueryWrapper.eq(FileImportFlowEntity::getFileImportId, fileImportId)
                .eq(FileImportFlowEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<FileImportFlowEntity> fileImportFlowList = baseMapper.selectList(fileImportFlowLambdaQueryWrapper);
        log.info("查询文件上传流水完成。size={}", fileImportFlowList.size());
        return fileImportFlowList;
    }

    @Override
    public void delete(List<Long> fileImportIdList) {
        if (CollectionUtils.isEmpty(fileImportIdList)) {
            log.info("删除文件上传流水，传递的文件上传记录主键列表为空");
            return;
        }
        // 获取操作用户
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        LambdaUpdateWrapper<FileImportFlowEntity> fileImportFlowLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        fileImportFlowLambdaUpdateWrapper.set(FileImportFlowEntity::getDeleteStatus, CommonEnum.ONE.getValue())
                .set(FileImportFlowEntity::getUpdateTime, date)
                .set(FileImportFlowEntity::getUpdateUser, userPo.getUsername())
                .set(FileImportFlowEntity::getUpdateUserId, userPo.getUserId());
        fileImportFlowLambdaUpdateWrapper.in(FileImportFlowEntity::getFileImportId, fileImportIdList)
                .eq(FileImportFlowEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 删除
        int count = baseMapper.update(fileImportFlowLambdaUpdateWrapper);
        log.info("删除文件上传流水完成。count={}", count);
    }
}